<?php
/*------------------------------------------------------------------------
# ------------------------------------------------------------------------
# Copyright (C) 2004-2006 J.O.O.M Solutions Co., Ltd. All Rights Reserved.
# @license - Copyrighted Commercial Software
# Author: J.O.O.M Solutions Co., Ltd
# Websites:  http://www.joomlart.com -  http://www.joomlancers.com
-------------------------------------------------------------------------*/

defined( '_JEXEC' ) or die( 'Restricted access' );

class JA_ContentSlide{

	var $_params = '';
	var $_db = '';
	var $_show_price = 0;
	var $_show_addtocart = 0;
	var $_typeproduct = 0;
	var $_numElem = 0;
	var	$_numberjump = 1;
	var	$_total = 0;
	var $_listPro = '';
	var $_now 		= '';
	var $_access 	= '';
	var $_nullDate 	='';
	var $_numChar = '';
	var $_link_titles = '';
	var $_width 	 = '';
	var $_height 	 = '';
	var $_showimages  = '';
	//var $_listPro = array();
	////////////

	function JA_ContentSlide($params, $database, $showtitle,$showreadmore,$showintrotext,$link_titles ,$numElem,$numberjump,$numChar,$xwidth,$xheight,$showimages){
		$mainframe =& JFactory::getApplication('site');
		$this->_params 			= 	$params;
		$this->_db 				= 	$database;
		$this->_showtitle 		= 	$showtitle;
		$this->_showreadmore	= 	$showreadmore;
		$this->_showintrotext 	= 	$showintrotext;
		$this->_link_titles 	= 	$link_titles;
		$this->_numElem 		= 	$numElem;
		$this->_numberjump 		= 	$numberjump;
		$this->_numChar 		= 	$numChar;
		$this->_width 			= 	$xwidth;
		$this->_height 			= 	$xheight;
		$this->_showimages 		= 	$showimages;

		$this->_now 			= date( 'Y-m-d H:i' );
		$this->_access 			= !$mainframe->getCfg( 'shownoauth' );
		$this->_nullDate 		= $this->_db->getNullDate();

		$this->_params->set( 'image', 		1 );
		$this->_params->set( 'intro_only', 		1 );
		$this->_params->set( 'hide_author', 		1 );
		$this->_params->set( 'hide_createdate', 	0 );
		$this->_params->set( 'hide_modifydate', 	1 );
		$this->_params->set( 'link_titles', 		1 );

	}

	function getTotal($catid){
		$my = &JFactory::getUser();
		$aid	= $my->get('aid', 0);
		$query 	= 	" SELECT COUNT(a.id)"
					. "\n FROM #__content AS a"
					. "\n WHERE a.state = 1"
					. "\n AND ( a.publish_up = " . $this->_db->Quote( $this->_nullDate ) . " OR a.publish_up <= " . $this->_db->Quote( $this->_now ) . " )"
					. "\n AND ( a.publish_down = " . $this->_db->Quote( $this->_nullDate ) . " OR a.publish_down >= " . $this->_db->Quote( $this->_now ) . " )"
					. ( $this->_access ? "\n AND a.access <= " . (int) $aid : '' )
					;
		if($catid) $query .= ' AND a.catid IN ('.$catid.') ';
		$this->_db->setQuery($query);
		$this->_total = $this->_db->loadResult();
		return $this->_total;
	}

	function loadContents($catid,$start=0,$numberE=1,$useajax = 1){
		$my = &JFactory::getUser();
		$aid	= $my->get('aid', 0);
		$query 	= 	'SELECT a.*,cc.description as catdesc, cc.title as cattitle,s.description as secdesc, s.title as sectitle,' .
					' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'.
					' CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(":",cc.id,cc.alias) ELSE cc.id END as catslug,'.
					' CASE WHEN CHAR_LENGTH(s.alias) THEN CONCAT_WS(":", s.id, s.alias) ELSE s.id END as secslug'
					. "\n FROM #__content AS a".
					' INNER JOIN #__categories AS cc ON cc.id = a.catid' .
					' INNER JOIN #__sections AS s ON s.id = a.sectionid'
					. "\n WHERE a.state = 1"
					. "\n AND ( a.publish_up = " . $this->_db->Quote( $this->_nullDate ) . " OR a.publish_up <= " . $this->_db->Quote( $this->_now ) . " )"
					. "\n AND ( a.publish_down = " . $this->_db->Quote( $this->_nullDate ) . " OR a.publish_down >= " . $this->_db->Quote( $this->_now ) . " )"
					. ( $this->_access ? "\n AND a.access <= " . (int) $aid : '' )
					;
		if($catid) $query .= ' AND a.catid IN ('.$catid.') ';
		if($useajax) $query .=  ' LIMIT '.$start.','.$numberE;
           //echo $query;
		$this->_db->setQuery($query);
		return $this->_listPro = $this->_db->loadObjectlist();
	}

	function genHTML($catid, $start = 0 ,$numElem = 4,$useajax){
		$mainframe =& JFactory::getApplication('site');
		//JPluginHelper::importPlugin( 'content' );

		$contents = $this->loadContents($catid,$start,$numElem,$useajax);
		$i=0;
		foreach($contents as $contn){
			echo "<div class=\"content_element\" style=\"float:left;\">";

				// get Itemid
				$Itemid =  $mainframe->getItemid( $contn->id );
				// Blank itemid checker for SEF
				if ($Itemid == NULL) {
					$Itemid = '';
				} else {
					$Itemid = '&amp;Itemid='. $Itemid;
				}
				
				$link =	JRoute::_(ContentHelperRoute::getArticleRoute($contn->slug, $contn->catslug, $contn->sectionid));
				if($this->_showtitle) {
					echo "<div class=\"ja_slidetitle\">";
					echo ($this->_link_titles) ? '<a href="'.$link.'" title="">'.$contn->title.'</a>' : $contn->title;
					echo "</div>";
				}


				$contn->text 	= $contn->introtext;
				$mainframe->triggerEvent( 'onPrepareContent', array( &$contn, &$this->_params, 0 ) , true );
				$contn->introtext = $contn->text;
				$image = $this->replaceImage ($contn, $this->_numChar,$this->_showimages, $this->_width, $this->_height);////////////////

				if($this->_showimages) {
				echo "<div class=\"ja_slideimages clearfix\">";
					echo '<a href="'.$link.'" title="">'.$image.'</a>';
				echo "</div>";
				}
				if($this->_showintrotext) {
				echo "<div class=\"ja_slideintro\">";
					echo ($this->_numChar) ?$contn->introtext1 : $contn->introtext;
				echo "</div>";
				}

				if($this->_showreadmore) {
				echo "<div class=\"ja-slidereadmore\">";
					echo '<a href="'.$link.'" class="readon">'._READ_MORE.'</a>';
				echo "</div>";
				}
			echo "</div>";
			$i++;
		}
	}

	function genHTML_AJAX($catid,$currentItem,$numberjump = 1){
		$mainframe =& JFactory::getApplication('site');

		//JPluginHelper::importPlugin( 'content' );

		$contn = $this->loadContents($catid,$currentItem,$numberjump);
			// get Itemid
			$Itemid = $mainframe->getItemid( $contn[0]->id);
			// Blank itemid checker for SEF
			if ($Itemid == NULL) {
				$Itemid = '';
			} else {
				$Itemid = '&amp;Itemid='. $Itemid;
			}
			
			$link =	JRoute::_(ContentHelperRoute::getArticleRoute($contn[0]->slug, $contn[0]->catslug, $contn[0]->sectionid));
			
			if($this->_showtitle) {
			echo "<div class=\"ja_slidetitle\">";
				echo ($this->_link_titles) ? '<a href="'.$link.'">'.$contn[0]->title.'</a>' : $contn[0]->title;
			echo "</div>";
			}

			$contn[0]->text 	= $contn[0]->introtext;
			$mainframe->triggerEvent( 'onPrepareContent', array( &$contn[0], &$this->_params, 0 ) , true );
			$contn[0]->introtext = $contn[0]->text;
			$image = $this->replaceImage ($contn[0], $this->_numChar,$this->_showimages, $this->_width, $this->_height);////////////////

			if($this->_showimages && $image) {
			echo "<div class=\"ja_slideimages clearfix\">";
				echo $image;
			echo "</div>";
			}
			if($this->_showintrotext) {
			echo "<div class=\"ja_slideintro\">";
				echo ($this->_numChar) ?$contn[0]->introtext1 : $contn[0]->introtext;
			echo "</div>";
			}

			if($this->_showreadmore) {
			echo "<div class=\"ja-slidereadmore\">";
				echo '<a href="'.$link.'" class="readon">'._READ_MORE.'</a>';
			echo "</div>";
			}
	}

	function replaceImage( &$row, $maxchars, $showimage, $width = 0, $height = 0 ) {
		//global $database, $_MAMBOTS, $current_charset;

		// expression to search for
		$regex = '/{mosimage\s*.*?}/i';

		$image = $showimage?$this->processImage ( $row, $width, $height ):"";
		if ($image) {
			$row->introtext = trim($row->introtext);
			$row->introtext = preg_replace( $regex, '', $row->introtext );
		} else {
			$regex = "/\<img.*\/\>/";
			preg_match ($regex, $row->introtext, $matches);
			$row->introtext = trim($row->introtext);
			$row->introtext = preg_replace ($regex, '', $row->introtext);

			$image = $showimage && count($matches)?$matches[0]:"";
			if ($image) {
				if ($width) {
					if (is_numeric ($width)) $width = $width;
					$regex = '/(width\s*=\s*\"?)([^\s,^\"]*)/';
					if (preg_match ($regex, $image))
						$image = preg_replace ($regex, '${1}'.$width, $image);
					else {
						$image = substr ($image, 0, -2) . " width=\"$width\" />";
					}
				}
				if ($height) {
					if (is_numeric ($height)) $height = $height;
					$regex = '/(height\s*=\s*\"?)([^\s,^\"]*)/';
					if (preg_match ($regex, $image))
						$image = preg_replace ($regex, '${1}'.$height, $image);
					else {
						$image = substr ($image, 0, -2) . " heigh=\"$height\" />";
					}
				}
			}
		}

		$row->introtext1 = strip_tags($row->introtext);
		if ($maxchars && strlen ($row->introtext) > $maxchars) {
			$row->introtext1 = substr ($row->introtext1, 0, $maxchars) . "...";
		}
		// clean up globals
		return $image;
	}

	function processImage ( &$row, $width, $height ) {
		/* for 1.5 - don't need to use image parameter */
		return 0;
		/* End 1.5 */
	}

}
?>
